﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.IO.Pipes;
using ComLib.ComType;
using LogLib;

namespace CoreBroker
{
    /// <summary>
    /// 照片检测客户端，负责和检测服务器通信的关键中间件
    /// </summary>
    public class PhotoDetectClient
    {
        private NamedPipeClientStream Client { get; set; }    
        private StringBuilder InputStr { get; set; }
        private string OutputStr { get; set; }

        public PhotoDetectClient(string serverName, string serverHost)
        {
            Client = new NamedPipeClientStream(serverHost, serverName);
            this.InputStr = new StringBuilder();
        }
 
        /// <summary>
        /// 向服务器请求处理数据
        /// </summary>
        /// <param name="inputtingArgs">请求参数数据结构Json格式</param>
        /// <returns>服务器处理结果，Json格式</returns>
        public string Request(string inputtingArgs)
        {
            this.OutputStr = inputtingArgs.Trim();
            try
            {
                this.Client.Connect(120000);
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                this.Client.Close();
                this.Client.Dispose();
                return "";
            }           
            StreamString ss = new StreamString(this.Client);
            ss.WriteString(OutputStr);
            this.Client.WaitForPipeDrain();
            this.InputStr.Append(ss.ReadString());
            this.Client.Close();
            this.Client.Dispose();
            return this.InputStr.ToString().Replace("\0", " ").Trim();
        }
    }
}

 //public static void Client2()
 //       {
 //           MyClient client = new MyClient("test_pipe", ".");
 //           Console.Write("请输入发送字符串：");
 //           Console.WriteLine("服务端输出：\r\n" + client.Request(Console.ReadLine()));
 //       }
